package com.ubaer.zsycz.ui.fragment;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lsw.Base.AdapterRefresh;
import com.lsw.pullableview.PullToRefreshLayout;
import com.lsw.pullableview.PullableListView;
import com.lsw.util.StringUtil;
import com.lsw.util.ToastUtil;
import com.ubaer.zsycz.R;
import com.ubaer.zsycz.inject.Constants;
import com.ubaer.zsycz.model.OrderModel;
import com.ubaer.zsycz.ui.activity.OrderDetailsActivity;
import com.ubaer.zsycz.ui.adapter.UntreatedAdapter;
import com.ubaer.zsycz.ui.base.fragment.BaseFragment;
import com.ubaer.zsycz.ui.dialog.ConfirmDialog;
import com.ubaer.zsycz.ui.dialog.RechargeProcessingDialog;
import com.ubaer.zsycz.ui.dialog.SuccessDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 未处理订单
 * Created by Administrator on 2018/9/25 0025.
 */

public class UntreatedFragment extends BaseFragment<UntreatedPresenter> implements PullToRefreshLayout.OnRefreshListener, AdapterRefresh, UntreatedContract.View {

    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout pullToRefreshLayout;
    @BindView(R.id.listview)
    PullableListView listview;
    UntreatedAdapter untreatedAdapter;
    //全选
    @BindView(R.id.cb_iall)
    CheckBox cb_iall;
    @BindView(R.id.tv_all)
    TextView tv_all;
    //共计
    @BindView(R.id.tv_number)
    TextView tv_number;
    //处理单笔订单
    RechargeProcessingDialog rechargeProcessingDialog;
    //处理批量订单
    ConfirmDialog confirmDialog;
    //处理批量订单
    @BindView(R.id.tv_all_order)
    TextView tv_all_order;
    //订单状态  0待处理 1充值成功 2充值失败
    String status = "0";
    int page = Constants.PAGE_START;
    AdapterRefresh adapterRefresh;
    @BindView(R.id.tvEmptyMsg)
    TextView tvEmptyMsg;
    //处理状态
    SuccessDialog successDialog;


    public void setAdapterRefresh(AdapterRefresh adapterRefresh) {
        this.adapterRefresh = adapterRefresh;
    }

    @Override
    protected int getlayoutId() {
        return R.layout.fragment_untreated;
    }

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected void initEventAndData() {
        successDialog = new SuccessDialog(mContext);
        rechargeProcessingDialog = new RechargeProcessingDialog(mContext);
        confirmDialog = new ConfirmDialog(mContext);
        pullToRefreshLayout.setOnRefreshListener(this);
        listview.setCanPulldown(true);
        untreatedAdapter = new UntreatedAdapter(mContext);
        listview.setAdapter(untreatedAdapter);
        untreatedAdapter.setAdapterRefresh(this);
        listview.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(mContext, OrderDetailsActivity.class);
            intent.putExtra("id", untreatedAdapter.getList().get(position).getId());
            startActivity(intent);
        });
        cb_iall.setOnClickListener(v -> {
                    untreatedAdapter.all(cb_iall.isChecked());
                    if (cb_iall.isChecked()) {
                        tv_number.setText(untreatedAdapter.getList().size() + "笔");
                    } else {
                        tv_number.setText("0笔");
                    }
                }
        );
        tv_all.setOnClickListener(v -> {
                    cb_iall.setChecked(!cb_iall.isChecked());
                    untreatedAdapter.all(cb_iall.isChecked());
                    if (cb_iall.isChecked()) {
                        tv_number.setText(untreatedAdapter.getList().size() + "笔");
                    } else {
                        tv_number.setText("0笔");
                    }
                }
        );

        tv_all_order.setOnClickListener(v ->
                {
                    if (!StringUtil.isNullOrEmpty(getids()))
                        confirmDialog.show(v1 -> {
                            confirmDialog.dismiss();
                            mPresenter.ProcessingOrders(getids());
                        });
                    else {
                        ToastUtil.showToast(mContext, "请选择订单！");
                    }
                }
        );
        mPresenter.getOrderList(status, "", page, Constants.PAGE_DEFAULT_COUNT + "");
    }

    @Override
    protected void lazyLoadData() {

    }

    @Override
    public void setOrderList(List<OrderModel> list, boolean ispage) {
        if (list == null)
            return;
        listview.setCanPullup(ispage);
        if (page == Constants.PAGE_START) {
            untreatedAdapter.setData(list);
        } else {
            untreatedAdapter.addData(list);
        }
        untreatedAdapter.isall();
        isData();
    }

    int remove;
    String sta;

    @Override
    public void setProcessingSucceed() {
        //处理成功
        untreatedAdapter.remove(remove);
        tv_number.setText("0笔");
        if (TextUtils.equals("1", sta)) {
            //充值成功
            if (adapterRefresh != null)
                adapterRefresh.onRefreshAdapter(3, 3);
        } else if (TextUtils.equals("2", sta)) {
            //充值失败
            if (adapterRefresh != null)
                adapterRefresh.onRefreshAdapter(4, 4);
        }
        isData();
        successDialog.show(1, "");
    }

    @Override
    public void setProcessingSucceeds() {
        tv_number.setText("0笔");
        page = Constants.PAGE_START;
        mPresenter.getOrderList(status, "", page, Constants.PAGE_DEFAULT_COUNT + "");
        if (adapterRefresh != null)
            adapterRefresh.onRefreshAdapter(3, 3);
        successDialog.show(1, "");
    }

    @Override
    public void setProcessingfail(String msg) {
        successDialog.show(2, msg);
    }

    @Override
    public void RefreshSucceed() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.SUCCEED);
    }

    @Override
    public void RefreshFail() {
        pullToRefreshLayout.Resultfinish(PullToRefreshLayout.FAIL);
    }

    @Override
    public void getRefresh() {
        onRefresh(null);
    }

    @Override
    public void openLogin() {
        OpenLogin();
    }

    @Override
    public void onRefreshAdapter(int position, int taskid) {
        if (taskid == 1) {
            //全选中
            cb_iall.setChecked(true);
            tv_number.setText(position + "笔");
        } else if (taskid == 2) {
            cb_iall.setChecked(false);
            tv_number.setText(position + "笔");
        } else if (taskid == 3) {
            rechargeProcessingDialog.show(untreatedAdapter.getList().get(position).getOrder_no(), new RechargeProcessingDialog.ProcessingResult() {
                @Override
                public void Result(String status, String reason) {
                    rechargeProcessingDialog.dismiss();
                    sta = status;
                    remove = position;
                    mPresenter.ProcessingOrder(untreatedAdapter.getList().get(position).getId(), status, reason);
                }
            });
        }

    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        page = Constants.PAGE_START;
        mPresenter.getOrderList(status, "", page, Constants.PAGE_DEFAULT_COUNT + "");
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        mPresenter.getOrderList(status, "", ++page, Constants.PAGE_DEFAULT_COUNT + "");
    }

    public String getids() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < untreatedAdapter.getList().size(); i++) {
            if (untreatedAdapter.getList().get(i).isIscheck()) {
                list.add(untreatedAdapter.getList().get(i).getId());
            }
        }
        if (list.size() > 0)
            return new Gson().toJson(list);
        else return "";
    }

    /**
     * 是否有未处理订单
     */
    public void isData() {
        if (untreatedAdapter.getList().size() > 0) {
            if (adapterRefresh != null)
                adapterRefresh.onRefreshAdapter(1, 1);
        } else {
            if (adapterRefresh != null)
                adapterRefresh.onRefreshAdapter(0, 0);
        }
        if (untreatedAdapter.getList().size() == 0) {
            pullToRefreshLayout.setNoData(View.VISIBLE);
        } else {
            pullToRefreshLayout.setNoData(View.INVISIBLE);
        }
    }
}
